import requests
from ddddocr import DdddOcr
from lxml import etree
import random,time

while True:
    ocr = DdddOcr(show_ad=False)

    res_img = requests.get(f'https://www.labnetwork.com.cn/gateway/basic/mall/oauth/code?{random.random()}')
    # print(res_img.json())
    img_data = res_img.json()['data']
    # tree = etree.HTML(res_img.json())['data']

    r = ocr.classification(img_data['img'][22:])



    res_login_post = requests.post("https://www.labnetwork.com.cn/gateway/basic/mall/oauth/login", json={
        'captchaCode': f'{r}',
        'captchaKey': f'{img_data["uuid"]}',
        'password': "Qikuedu123456",
        'userEmail': "2026793427@qq.com"
    })

    if "验证码错误" in res_login_post.text:
        print("验证码错误")
    else:
        # print(res_login_post.json())
        token = res_login_post.json()['data']['userInfo']['token']
        break

# 3. 使用token 进行翻页
time.sleep(1)
res = requests.post('https://www.labnetwork.com.cn/gateway/oldapi/mall/chemical/products', json={
    "query": "",
    "country": "CN",
    "pageSize": 10,
    "chemIds": [],
    "suppliers": [],
    "rank": 0,
    "packSize": "",
    "packUnit": "",
    "unitStock": "",
    "selectedUnit": "",
    "molecularWeightLow": "",
    "molecularWeightHigh": "",
    "locations": [],
    "startRecord": 0,
    "modelRange": [
        84.11,
        1149.73
    ]
}, headers={
    'token': f'{token}'
})

print(res.json())



